<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>vue</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
    <style>
        span.result {
            display: block;
            height: 50px;
            width: 100%;
            box-sizing: border-box;
            text-align: right;
            font-size: 24px;
            line-height: 50px;
            border:1px solid #CCC;
        }
        td.btn {
            font-weight: bold;
            width: 50px;
            height: 50px;
            font-size: 24px;
            text-align: center;
            line-height: 50px;
            cursor: pointer;
            user-select: none;
        }
        td.cel-btn {
            background-color: #999;
            color: blue;
        }
        td.num-btn {
            background-color: #eee;
            color: #333;
        }
    </style>
</head>
<body>
    <div id="demo">
        <table>
            <tbody>
                <tr>
                    <th colspan="4">
                        <span class="result">{{result}}</span>
                    </th>
                </tr>
                <tr>
                    <td class="btn cel-btn" @click="reset">C</td>
                    <td class="btn cel-btn" @click="appendCelChar">/</td>
                    <td class="btn cel-btn" @click="appendCelChar">*</td>
                    <td class="btn cel-btn" @click="back">←</td>
                </th>
                <tr>
                    <td class="btn num-btn" @click="appendNum">7</td>
                    <td class="btn num-btn" @click="appendNum">8</td>
                    <td class="btn num-btn" @click="appendNum">9</td>
                    <td class="btn cel-btn" @click="appendCelChar">-</td>
                </tr>
                <tr>
                    <td class="btn num-btn" @click="appendNum">4</td>
                    <td class="btn num-btn" @click="appendNum">5</td>
                    <td class="btn num-btn" @click="appendNum">6</td>
                    <td class="btn cel-btn" @click="appendCelChar">+</td>
                </tr>
                <tr>
                    <td class="btn num-btn" @click="appendNum">1</td>
                    <td class="btn num-btn" @click="appendNum">2</td>
                    <td class="btn num-btn" @click="appendNum">3</td>
                    <td class="btn cel-btn" @click="cal" rowspan="2">=</td>
                </tr>
                <tr>
                    <td class="btn num-btn" @click="appendCelChar">%</td>
                    <td class="btn num-btn" @click="appendNum">0</td>
                    <td class="btn num-btn" @click="appendCelChar">.</td>
                </tr>
            </tbody>
        </table>
    </div>

    <script>
        let vue = new Vue({
            el: '#demo',
            data: {
                result: '',
                celChars: ['%', '.', '/', '*', '-', '+'],
            },
            methods:{
                appendCelChar: function(event) {
                    if (this.result != '' && !this._endWithCelChar()) {
                        this._appendChar(event);
                    }
                },
                appendNum: function(event) {
                    this._appendChar(event);
                },
                _endWithCelChar: function(event) {

                    let lastChar = this.result.substring(this.result.length - 1);
                    for(let index in this.celChars) {
                        if (this.celChars[index] == lastChar) {
                            return true;
                        }
                    }
                    return false;
                },
                _appendChar: function(event) {
                    let _char = event.target.innerText;
                    this.result = this.result + _char;
                },
                back: function(event) {
                    this.result = this.result.substring(0, this.result.length - 1);
                },
                reset: function(event) {
                    this.result = "";
                },
                cal: function(event) {
                    let num = eval(this.result);
                    this.result = num.toString();
                }
            }
        });    
    </script>
</body>
</html>